export class DrawingSurfaceHelper {
  static create(ctx: CanvasRenderingContext2D) {
    return new DrawingSurfaceHelper(ctx);
  }

  private canvas: HTMLCanvasElement;
  private snapshot: ImageData | undefined;
  constructor(protected ctx: CanvasRenderingContext2D) {
    this.canvas = ctx.canvas;
  }

  /**
   * 创建快照
   */
  takeSnapshot() {
    this.snapshot = this.ctx.getImageData(
      0,
      0,
      this.canvas.width,
      this.canvas.height
    );
  }
  /**
   * 恢复快照
   */
  restoreSnapshot() {
    if (this.snapshot) {
      this.ctx.putImageData(this.snapshot, 0, 0);
    }
  }

  /**
   * 清除快照
   */
  clearSnapshot() {
    this.snapshot = undefined;
  }
}
